<template>
    <div class="app-container">
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
                <el-select
                    v-model="searchObj.provinceCode"
                    placeholder="请选择省"
                        @change="provinceChanged">
                    <el-option
                        v-for="item in provinceList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"/>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-select
                v-model="searchObj.cityCode"
                placeholder="请选择市"
                >
                    <el-option
                    v-for="item in cityList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"/>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-input v-model="searchObj.hosname" placeholder="医院名称"/>
            </el-form-item>

            <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>


            <el-button type="default" @click="resetData()">清空</el-button>
        </el-form>

        <!-- banner列表 -->
        <el-table v-loading="listLoading" :data="list"
                border
              fit
              highlight-current-row>

            <el-table-column
            label="序号"
            width="100"
            align="center">
                <template slot-scope="scope">
                        {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column label="医院logo">
                <template slot-scope="scope">
                <img :src="'data:image/jpeg;base64,'+scope.row.logoData" width="80">
                </template>
            </el-table-column>

            <el-table-column prop="hosname" label="医院名称"/>
            <el-table-column prop="param.hostype" label="等级" width="90"/>
            <el-table-column prop="param.fullAddress" label="详情地址"/>

            <el-table-column label="状态" width="130">
                <template slot-scope="scope">
                        {{ scope.row.status === 0 ? '未上线' : '已上线' }}
                </template>
            </el-table-column>
        
            <el-table-column prop="createTime" label="创建时间"/>

            <el-table-column label="操作" width="430" align="center">
                <template slot-scope="scope">

                    <router-link :to="'/hospitalSet/hospital/show/'+scope.row.id">
                        <el-button type="primary" size="mini">查看详情</el-button>
                    </router-link>

                    <router-link :to="'/hospitalSet/hospital/schedule/'+scope.row.hoscode">
                        <el-button type="primary" size="mini">排班信息</el-button>
                    </router-link>

                    <el-button v-if="scope.row.status == 1"  type="primary" size="mini" @click="updateStatus(scope.row.id, 0)">下线</el-button>
                    <el-button v-if="scope.row.status == 0"  type="danger" size="mini" @click="updateStatus(scope.row.id, 1)">上线</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <!--  -->
        <el-pagination
            :current-page="page"
            :total="total"
            :page-size="limit"
            :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
            style="padding: 30px 0; text-align: center;"
            layout="sizes, prev, pager, next, jumper, ->, total, slot"
            @current-change="fetchData"
            @size-change="changeSize"
        />
    </div>
</template>

<script>
import hospitalApi from '@/api/hosp'

export default {
    data(){
        return{
            listLoading: true, // 数据是否正在加载 <!-- banner列表 -->下显示数据是否在加载
            list: null, // banner列表，每一页数据的集合显示
            total: 0, // 数据库中的总记录数
            page: 1, // 默认页码
            limit: 10, // 每页记录数
            searchObj: {}, // 查询表单对象
            provinceList: [],//省列表
            cityList: [],//市列表
            districtList: []//地区列表
        }
    },
    created(){
        //查询医院列表
        this.fetchData()
        //查询全部省份
        this.queryAllProvince()
    },
    methods:{
        //更新医院上线状态
        updateStatus(id,status){
            hospitalApi.updateHospitalStatus(id,status)
                .then(response=>{
                    //刷新当前页面
                    this.fetchData(this.page)      
                })
        },
        //医院信息列表
        fetchData(page=1){
            this.page=page
                hospitalApi.getHospiralList(this.page,this.limit,this.searchObj)
                    .then(response=>{
                        this.list=response.data.content //获取到每页的数据显示   
                        this.total=response.data.totalElements//获取到总的记录数
                        this.listLoading=false //加载完毕表示加载已经结束
            })
        },
        //查询全部省份
        queryAllProvince(){
            hospitalApi.queryByDictCode('Province')
                .then(response=>{
                    this.provinceList = response.data //返回省的列表
            })
        },
        //联动查询省下的市
        provinceChanged(){
            //初始化操作
            this.cityList =null
            this.searchObj.cityCode=null     
            //调用方法，根据id查询后面节点
            hospitalApi.queryChildId(this.searchObj.provinceCode)
                .then(response=>{
                    this.cityList =response.data 
                })
        },
        // 当页码发生改变的时候
        changeSize() {
            this.limit = size //这个size指的是:page-sizes="[5, 10, 20, 30, 40, 50, 100]"里面可以选择的项目
            this.fetchData(1)
        },
    }
}
</script>

